package com.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class UrlUtil {
	private static Logger log = Logger.getLogger(UrlUtil.class);
	
	public static String escape(String src) {
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length() * 6);
		for (i = 0; i < src.length(); i++) {
			j = src.charAt(i);
			if (Character.isDigit(j) || Character.isLowerCase(j)
					|| Character.isUpperCase(j))
				tmp.append(j);
			else if (j < 256) {
				tmp.append("%");
				if (j < 16)
					tmp.append("0");
				tmp.append(Integer.toString(j, 16));
			} else {
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

	public static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				
					if (src.charAt(pos + 1) == 'u') {
						try{
						ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
						tmp.append(ch);
						lastPos = pos + 6;
						}catch(Exception e){
							//log.info(e.getMessage());
							tmp.append(src.substring(pos + 2, pos + 6));
						}
					} else {
						try{
							ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
							tmp.append(ch);
							lastPos = pos + 3;
						}catch(Exception e){
							//log.info(e.getMessage());
							tmp.append(src.substring(pos + 1, pos + 3));
						}
					}
				
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}
	private static String getContentByUrl(String strUrl) {
		if(strUrl.endsWith(".shtml")||strUrl.endsWith(".html")||strUrl.endsWith(".htm")){
		StringBuffer sb = new StringBuffer("");
		try {
			URL url = new URL(strUrl);
			URLConnection con = url.openConnection();
			con.setConnectTimeout(10*1000);
			BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String s = "";
			while ((s = br.readLine()) != null) {
				sb.append(s);
			}
			br.close();
			return HTMLSpirit.delHTMLTag(sb.toString());
		} catch (Exception e) {
			log.error("打开页面["+strUrl+"]失败=="+e.getMessage(),e);
			return "";
		}
		}else{
			return strUrl;
		}
	}
	

	public static String getUrl(String u) {
		Pattern p = Pattern
				.compile("/{0,1}[&\\?]{1}(onecgy=){1}[\\.a-zA-Z0-9_\\-]+");
		Matcher m = p.matcher(u);
		String t = "";
		StringBuffer tu = new StringBuffer();
		if (m.find()) {
			t = m.group();
			p = Pattern.compile("/{0,1}[&\\?]{1}(onecgy=){1}");
			m = p.matcher(t);
			tu.append(m.replaceAll(""));
			tu.append("/");
		}
		p = null;
		m = null;
		p = Pattern.compile("[\\?&]{0,1}(twocgy=){1}[\\.a-zA-Z0-9_\\-]+");
		m = p.matcher(u);
		if (m.find()) {
			t = m.group();
			p = Pattern.compile("[\\?&]{0,1}(twocgy=){1}");
			m = p.matcher(t);
			tu.append(m.replaceAll(""));
			tu.append("/");
		}
		p = null;
		m = null;
		// System.out.println(u);
		p = Pattern.compile("(/log\\.jsp\\?file=){1}[\\.a-zA-Z0-9_\\-]+");
		m = p.matcher(u);
		if (m.find()) {
			t = m.group();
			p = Pattern.compile("(/log\\.jsp\\?file=){1}");
			m = p.matcher(t);
			tu.append(m.replaceAll(""));
			tu.append(".htm");
		}
		return "/" + tu.toString();
	}

	public static String getCurUri(HttpServletRequest request) {
		String qStr = request.getQueryString();
		String rtn = request.getRequestURI();
		// rtn.append(request.getRequestURI());
		if (qStr != null) {
			rtn += ("?" + qStr);
		}
		String ur = getCurUr(request);
		if (qStr != null && !qStr.equals("")) {
			if (qStr.indexOf("&") != -1)
				rtn = ur.substring(ur.indexOf("/"), ur.indexOf("&")).toString();
			else
				rtn = ur;
		}
		return rtn.toString();
	}

	public static String getCurUr(HttpServletRequest request) {
		String qStr = request.getQueryString();
		StringBuffer rtn = new StringBuffer();
		rtn.append(request.getRequestURI());
		if (qStr != null) {
			rtn.append("?" + qStr);
		}
		return rtn.toString();
	}
}
